package com.ebuy.shopping.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ebuy.shopping.model.Order;
import com.ebuy.shopping.service.IOrderService;

@Controller
@RequestMapping("/order")
public class OrderController
{
    @Autowired
    private IOrderService orderService;

    @RequestMapping(value = "{id}/show", method = RequestMethod.GET)
    public String show(@PathVariable
    int id, Model model) throws Exception
    {
        Order order = orderService.load(id);
        model.addAttribute("order", order);
        return "order/show"; // -->webapp/WEB-INF/pages/user/show.jsp
    }

    @RequestMapping(value = "{id}/update", method = RequestMethod.GET)
    public String toUpdate(@PathVariable
    int id, Model model) throws Exception
    {
        Order order = orderService.load(id);
        model.addAttribute("order", order);
        return "order/order_update"; // -->webapp/WEB-INF/pages/user/show.jsp
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(int id, String userName, String userAddress)
            throws Exception
    {
        Order order = orderService.load(id);
        order.setUserAddress(userAddress);
        order.setUserName(userName);
        orderService.update(order);
        return "redirect:/order/list";
    }

    @RequestMapping(value = "{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable
    int id, Model model) throws Exception
    {
        orderService.deleteByPrimaryKey(id);
        return "forward:/order/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) throws Exception
    {
        List<Order> orders = orderService.getOrders();
        model.addAttribute("orders", orders);
        return "order/order"; // -->webapp/WEB-INF/pages/user/list.jsp
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() throws Exception
    {
        return "order/add"; // -->webapp/WEB-INF/pages/user/add.jsp
    }
}
